*------------------------------------------------------------------------------*
						*** TABLE S8. Balance tests ***
*------------------------------------------------------------------------------*

***PROGRAM FOR TURNOUT
cap program drop reg_tab_balance
program define reg_tab_balance, sclass
	args cmd spec 
	/* ARGUMENTS
	cmd: command (reg or areg)
	spec: number of the specification 
	*/

	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $demo_econ_controls // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $demo_econ_controls
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2_`spec'_`i'  = e(r2) // r-squared
		scalar N_`spec'_`i'  = e(N) // N
		scalar YM_`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***MACROS
local weather_controls "O3_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km"
global demo_econ_controls "pop_tot GDP_capita employment"

// THE FOLLOWING ARE ALL THE SPECIFICATIONS WE RUN (X-VARIABLES PLUS OPTIONS)

// Turnout. Unconditional relationship PM10
global spec5 "PM10_30km_10ug, ${hdfe_opt}, if samplePM10_allcontrols == 1"
// Turnout. PM10 with Ozone and weather controls, demographic and economic controls, and municipality, date and type fixed effects
global spec6 "PM10_30km_10ug `weather_controls', ${hdfe_opt}, if samplePM10_allcontrols == 1"

*------------------------------------------------------------------------------*

use "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

***Regressions
*Tunrount 10s micrograms
forvalues j=5/6 {
	reg_tab_balance reghdfe `j'
}

*------------------------------------------------------------------------------*

***Generate table
gen str table=""

//FIRST PANEL: WITHOUT CONTROLS
// PM10 coefficients
local i=1
local line "PM10 ($10\mu g / m^3$)&"
local line "`line'" $f_b_bt bPM10_5_1 "\sym{$starPM10_5_1}" "&" $f_b_bt bPM10_5_2 "\sym{$starPM10_5_2}" "&" $f_b_bt bPM10_5_3 "\sym{$starPM10_5_3}"  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 s.e.
local i=`i'+1
local line "&" "(" $f_se sePM10_5_1 ")" "&" "(" $f_se sePM10_5_2 ")" "&" "(" $f_se sePM10_5_3 ")"  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Y mean
local i=`i'+1
local line "Mean dep. var.&"
local line "`line'" $f_YM_bt YM_5_1 "&" $f_YM_bt YM_5_2 "&" $f_YM_bt YM_5_3  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// R2
local i=`i'+1
local line "R$^2$ & "
local line "`line'" $f_r2 r2_5_1 "&" $f_r2 r2_5_2 "&" $f_r2 r2_5_3  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Observations
local i=`i'+1
local line "N&"
local line "`line'" $f_N N_5_1 "&" $f_N N_5_2 "&" $f_N N_5_3  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// CONTROLS
local i=`i'+1
qui replace table="\multicolumn{4}{l}{\textit{Controls} } \\ " in `i'
local i=`i'+1
qui replace table="\ \ County FE & \checkmark & \checkmark & \checkmark  \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Date FE & \checkmark & \checkmark & \checkmark  \\" in `i'
local i=`i'+1
qui replace table="\hline" in `i'

//SECOND PANEL: WITH CONTROLS
local i=`i'+1
qui replace table="\multicolumn{4}{l}{\textbf{B. With controls} } \\ " in `i'
// PM10 coefficients
local i=`i'+1
local line "PM10 ($10\mu g / m^3$)&"
local line "`line'" $f_b_bt bPM10_6_1 "\sym{$starPM10_6_1}" "&" $f_b_bt bPM10_6_2 "\sym{$starPM10_6_2}" "&" $f_b_bt bPM10_6_3 "\sym{$starPM10_6_3}"  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 s.e.
local i=`i'+1
local line "&" "(" $f_se sePM10_6_1 ")" "&" "(" $f_se sePM10_6_2 ")" "&" "(" $f_se sePM10_6_3 ")"  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Y mean
local i=`i'+1
local line "Mean dep. var.&"
local line "`line'" $f_YM_bt YM_6_1 "&" $f_YM_bt YM_6_2 "&" $f_YM_bt YM_6_3  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// R2
local i=`i'+1
local line "R$^2$ & "
local line "`line'" $f_r2 r2_6_1 "&" $f_r2 r2_6_2 "&" $f_r2 r2_6_3  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Observations
local i=`i'+1
local line "N&"
local line "`line'" $f_N N_6_1 "&" $f_N N_6_2 "&" $f_N N_6_3  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
local i=`i'+1
// CONTROLS
local i=`i'+1
qui replace table="\multicolumn{4}{l}{\textit{Controls} } \\ " in `i'
local i=`i'+1
qui replace table="\ \ County FE & \checkmark & \checkmark & \checkmark  \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Date FE & \checkmark & \checkmark & \checkmark  \\" in `i'
local i=`i'+1
qui replace table="\ \ Weather & \checkmark & \checkmark & \checkmark  \\" in `i'
local i=`i'+1
qui replace table="\ \ Ozone & \checkmark & \checkmark & \checkmark  %\\" in `i'
local i=`i'+1

**Create .tex file
outfile table using "${outputs}table_S8.tex" in 1/`i', replace noquote
drop table
macro drop star*

*------------------------------------------------------------------------------*

clear

exit
